﻿package com.athena.controller
{	
	import com.athena.vo.Chess;
	import com.athena.process.*;
	public class M_Controller {
		
		function M_Controller() { }
		public static function CheckTuong(hor : int, ver : int, csCheck : int) {
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Status == 1) continue ;
				if ((ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Ver == ver)) {
					if (ProcessInTable.currTable.ArrPlayers[csCheck].ArrChess[i].Symbol == "Tg") return true ;
					else return false ;
				}							
			}
			return false ;
		}
		public static function Check_ChieuTuong(obj : Chess, csCheck : int) : Boolean {
			var hor : int = obj.Hor ;
			var ver : int = obj.Ver ;
			//Tren Phai
			if ((hor > 1) && (ver < 8)) {
				if (ProcessInTable.ArrCheck[hor - 1][ver] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 2][ver + 1] != 0) && (ProcessInTable.ArrCheck[hor - 2][ver + 1] != obj.Color))
						if (CheckTuong(hor - 2, ver + 1, csCheck)) return true ;
				}
			}
			//Tren Trai
			if ((hor > 1) && (ver > 0)) {
				if (ProcessInTable.ArrCheck[hor - 1][ver] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 2][ver - 1] != 0) && (ProcessInTable.ArrCheck[hor - 2][ver - 1] != obj.Color))
						if (CheckTuong(hor - 2, ver - 1, csCheck)) return true ;
				}
			}
			//Trai Tren
			if ((hor > 0) && (ver > 1)) {
				if (ProcessInTable.ArrCheck[hor][ver - 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 1][ver - 2] != 0) && (ProcessInTable.ArrCheck[hor - 1][ver - 2] != obj.Color))
						if (CheckTuong(hor - 1, ver - 2, csCheck)) return true ;
				}
			}
			//Trai duoi
			if ((hor < 9) && (ver > 1)) {
				if (ProcessInTable.ArrCheck[hor][ver - 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 1][ver - 2] != 0) && (ProcessInTable.ArrCheck[hor + 1][ver - 2] != obj.Color))
						if (CheckTuong(hor + 1, ver -2, csCheck)) return true ;
				}
			}
			//Duoi Trai
			if ((hor < 8) && (ver > 0)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 2][ver - 1] != 0) && (ProcessInTable.ArrCheck[hor + 2][ver - 1] != obj.Color))
						if (CheckTuong(hor + 2, ver - 1, csCheck)) return true ;
				}
			}
			//Duoi Phai
			if ((hor < 8) && (ver < 8)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 2][ver + 1] != 0) && (ProcessInTable.ArrCheck[hor + 2][ver + 1] != obj.Color))
						if (CheckTuong(hor + 2, ver + 1, csCheck)) return true ;
				}
			}
			//Phai Duoi
			if ((hor < 9) && (ver < 7)) {
				if (ProcessInTable.ArrCheck[hor][ver + 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor + 1][ver + 2] != 0) && (ProcessInTable.ArrCheck[hor + 1][ver + 2] != obj.Color))
						if (CheckTuong(hor + 1, ver + 2, csCheck)) return true ;
				}
			}
			//Phai Tren
			if ((hor > 0) && (ver < 7)) {
				if (ProcessInTable.ArrCheck[hor][ver + 1] == 0) {
					if ((ProcessInTable.ArrCheck[hor - 1][ver + 2] != 0) && (ProcessInTable.ArrCheck[hor - 1][ver + 2] != obj.Color)) {
						if (CheckTuong(hor - 1, ver + 2, csCheck)) return true ;
					}
				}
			}
			return false ;
		}
		public static function Search_Chess(hor : int, ver : int) {
			for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess.length; i++) 
			{
				if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Status == 1) continue ;
				if ((ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Hor == hor) && (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Ver == ver)) {
					ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].gotoAndStop(3) ;
					if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[i].Symbol == "Tg") {
						ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.rotation = 180 ;
						ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.x = 0 ;
						ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[i].Xet_mc.y = 15 ;
					}
					break ;
				}							
			}
		}
		
		public static function Valid(obj : Chess) {
			var hor : int = obj.Hor ;
			var ver : int = obj.Ver ;
			//Tren Phai
			if ((hor > 1) && (ver < 8)) {
				if (ProcessInTable.ArrCheck[hor - 1][ver] == 0) {
					if (ProcessInTable.ArrCheck[hor - 2][ver + 1] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor - 2).toString() + (ver + 1).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor - 2).toString() + (ver + 1).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor - 2][ver + 1] != obj.Color)
						Search_Chess(hor - 2, ver + 1) ;
				}
			}
			//Tren Trai
			if ((hor > 1) && (ver > 0)) {
				if (ProcessInTable.ArrCheck[hor - 1][ver] == 0) {
					if (ProcessInTable.ArrCheck[hor - 2][ver - 1] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor - 2).toString() + (ver - 1).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor - 2).toString() + (ver - 1).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor - 2][ver - 1] != obj.Color)
						Search_Chess(hor - 2, ver - 1) ;
				}
			}
			//Trai Tren
			if ((hor > 0) && (ver > 1)) {
				if (ProcessInTable.ArrCheck[hor][ver - 1] == 0) {
					if (ProcessInTable.ArrCheck[hor - 1][ver - 2] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor - 1).toString() + (ver - 2).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor - 1).toString() + (ver - 2).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor - 1][ver - 2] != obj.Color)
						Search_Chess(hor - 1, ver - 2) ;
				}
			}
			//Trai duoi
			if ((hor < 9) && (ver > 1)) {
				if (ProcessInTable.ArrCheck[hor][ver - 1] == 0) {
					if (ProcessInTable.ArrCheck[hor + 1][ver - 2] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor + 1).toString() + (ver - 2).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor + 1).toString() + (ver - 2).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor + 1][ver - 2] != obj.Color) 
						Search_Chess(hor + 1, ver -2) ;
				}
			}
			//Duoi Trai
			if ((hor < 8) && (ver > 0)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver] == 0) {
					if (ProcessInTable.ArrCheck[hor + 2][ver - 1] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor + 2).toString() + (ver - 1).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor + 2).toString() + (ver - 1).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor + 2][ver - 1] != obj.Color)
						Search_Chess(hor + 2, ver - 1) ;
				}
			}
			//Duoi Phai
			if ((hor < 8) && (ver < 8)) {
				if (ProcessInTable.ArrCheck[hor + 1][ver] == 0) {
					if (ProcessInTable.ArrCheck[hor + 2][ver + 1] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor + 2).toString() + (ver + 1).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor + 2).toString() + (ver + 1).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor + 2][ver + 1] != obj.Color)
						Search_Chess(hor + 2, ver + 1) ;
				}
			}
			//Phai Duoi
			if ((hor < 9) && (ver < 7)) {
				if (ProcessInTable.ArrCheck[hor][ver + 1] == 0) {
					if (ProcessInTable.ArrCheck[hor + 1][ver + 2] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor + 1).toString() + (ver + 2).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor + 1).toString() + (ver + 2).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor + 1][ver + 2] != obj.Color)
						Search_Chess(hor + 1, ver + 2) ;
				}
			}
			//Phai Tren
			if ((hor > 0) && (ver < 7)) {
				if (ProcessInTable.ArrCheck[hor][ver + 1] == 0) {
					if (ProcessInTable.ArrCheck[hor - 1][ver + 2] == 0) {
						Cocaro.mainStage_mc["Checked" + (hor - 1).toString() + (ver + 2).toString() + "_mc"].visible = true ;
						Cocaro.mainStage_mc["Checked" + (hor - 1).toString() + (ver + 2).toString() + "_mc"].over_mc.visible = false ;
					} else if (ProcessInTable.ArrCheck[hor - 1][ver + 2] != obj.Color) {
						Search_Chess(hor - 1, ver + 2) ;
					}
				}
			}
		}
	}
}